package com.deke.system.service.impl;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.deke.entity.AclRole;
import com.deke.entity.AclUserRole;
import com.deke.system.dao.AclRoleDao;
import com.deke.system.service.AclRoleService;
import com.deke.system.service.AclUserRoleService;
import com.deke.system.vo.FenRoleVo;
import com.deke.system.vo.RoleVo;
import com.deke.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * (AclRole)表服务实现类
 *
 * @author makejava
 * @since 2022-12-04 14:06:25
 */
@Service
public class AclRoleServiceImpl implements AclRoleService {
    @Autowired
    private AclRoleDao aclRoleDao;

    @Autowired
    private AclUserRoleService userRoleService;

    /**
     * @create by: Username 刘亦辰 
     * @description: 角色分页查询 
     * @create time: 2022/12/4 14:12
     * @param null
     * @return :  
     **/
    @Override
    public Result<IPage<AclRole>> queryByConditionPage(Integer current, Integer pageSize, RoleVo roleVo){
        IPage<AclRole> page = new Page(current,pageSize);
        QueryWrapper<AclRole> wrapper = new QueryWrapper<>();
        if (StringUtils.hasText(roleVo.getRoleName())){
            wrapper.like("role_name",roleVo.getRoleName());
        }
        if (StringUtils.hasText(roleVo.getIsDeleted())){
            wrapper.eq("is_deleted",roleVo.getIsDeleted());
        }
        IPage<AclRole> ipage = aclRoleDao.selectPage(page, wrapper);
        return new Result<>(2000,"查询成功",ipage);
    }

    /**
     * @create by: Username 刘亦辰 
     * @description: 添加角色 
     * @create time: 2022/12/4 14:32
     * @param null
     * @return :  
     **/
    @Override
    public Result addRole(RoleVo roleVo) {
        AclRole aclRole = new AclRole();
        if (StringUtils.hasText(roleVo.getRoleName())){
            aclRole.setRoleName(roleVo.getRoleName());
        }
        if (StringUtils.hasText(roleVo.getRemark())){
            aclRole.setRemark(roleVo.getRemark());
        }
        aclRole.setGmtCreate(LocalDateTime.now());
        aclRole.setGmtModified(LocalDateTime.now());
        int insert = aclRoleDao.insert(aclRole);
        if (insert>0){
            return new Result<AclRole>(2000,"添加成功");
        }
        return null;
    }


    /**
     * @create by: Username 刘亦辰 
     * @description: 查询用户角色 
     * @create time: 2022/12/5 19:08
     * @param null
     * @return :  
     **/
    @Override
    public Result<List<AclRole>> findRoleByUserId(String id) {
        //查询所有角色
        List<AclRole> aclRoles = aclRoleDao.selectList(null);
        //查询用户角色
        List<AclRole> userRoles = aclRoleDao.findRoleByUserId(id);
        List<String> userRolesId = userRoles.stream()
                .map(item -> item.getId()).collect(Collectors.toList());
            Map map = new HashMap<>();
            map.put("userRoles",aclRoles);
            map.put("userRolesId",userRolesId);
            return new Result(2000,"查询成功",map);
    }

    @Override
    public Result fenUserRole(FenRoleVo fenRoleVo) {
        //先删除原有的权限
        aclRoleDao.deleteByUserId(fenRoleVo.getUserId());
        //在添加新的权限
        List<String> roleIds = fenRoleVo.getRoleIds();
        List<AclUserRole> userRoleList = new ArrayList<>();
        for (String roleId : roleIds) {
            AclUserRole aclUserRole = new AclUserRole(fenRoleVo.getUserId(), roleId, "0", LocalDateTime.now(), LocalDateTime.now());
            userRoleList.add(aclUserRole);
        }
        boolean b = userRoleService.saveBatch(userRoleList);
           return new Result(2000,"分配成功");
    }

    @Override
    public Result addOrUpdateRole(AclRole aclRole) {
        int update = aclRoleDao.updateById(aclRole);

        if (update>0){
            return  new Result(2000,"修改成功");
        }
        return new Result(0000,"修改失败");
    }

    @Override
    public Result updateRoleStatusByid(String id, String deleted) {
        Integer i = aclRoleDao.updateRoleStatusByid(id, deleted);
        if (i>0){
            return new Result(2000,"更改成功");
        }
        return new Result(0000,"更改失败");
    }

    /**
     * @create by: Username 刘亦辰 
     * @description: 角色管理  角色删除的时候对应的权限也应该删除 
     * @create time: 2022/12/6 0:02
     * @param null
     * @return :  
     **/
    @Transactional
    @Override
    public Result deleteRoleById(String roleId) {
        //删除角色前应该判断该角色是否被其他用户分配
        AclUserRole aclUserRole = aclRoleDao.queryRoleByRoleId(roleId);
        if (ObjectUtil.isNotNull(aclUserRole)){
            return new Result(0000,"角色已被分配,无法删除");
        }
        //删除角色前把角色对应的权限删除
        aclRoleDao.deletePermissionByRoleId(roleId);
        Integer integer = aclRoleDao.deleteRoleById(roleId);
        if (integer > 0){
            return new Result(2000,"删除成功");
        }
        return new Result(0000,"删除失败");
    }


}
